import os
import sys

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QFileDialog, QMessageBox, QWidget

from ..config import R
from ..core.excel import ExcelAtuoInput
from .autoinput_ui import Ui_Form

class AutoInputWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.parent = parent
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.ui.pushButton_SelectFile.clicked.connect(self.SelectFile)
        self.ui.pushButton_Subscription.clicked.connect(self.Subscription)
        self.ui.pushButton_Refund.clicked.connect(self.Refund)
        self.InitUi()
        self.filename = os.path.join(os.path.expanduser("~"), r'Desktop\temp')

    def InitUi(self):
        self.ui.lineEdit_FileName.setText(R.A_FileName)
        self.ui.lineEdit_SheetName.setText(R.A_SheetName)
        self.ui.checkBox_FirstClient.setChecked(R.A_FirstClient)
        self.ui.checkBox_IsAdded.setChecked(R.A_IsAdd)
        self.ui.lineEdit_ClientRow.setText(R.A_ClientRow)
        self.ui.lineEdit_CodeCol.setText(R.A_CodeCol)
        self.ui.lineEdit_CodeBits.setText(R.A_CodeBits)
        self.ui.lineEdit_StartRow.setText(R.A_StartRow)
        self.ui.lineEdit_EndRow.setText(R.A_EndRow)
        self.ui.lineEdit_StartColumn.setText(R.A_StartCol)
        self.ui.lineEdit_EndColumn.setText(R.A_EndCol)

    def SelectFile(self):
        filename, ext = QFileDialog.getOpenFileName(
            self,
            "OpenFile",
            os.path.dirname(self.filename),
            "Excel File (*.xlsx);; All File (*.*)",
        )
        if filename:
            self.ui.lineEdit_FileName.setText(filename)
            self.filename = filename
            self.parent.host.excel_filename = filename
        else: pass 
            # QMessageBox.about(self, "Open file", "没有选择文件!")

    def Subscription(self):
        '''
        征订录入
        '''
        try:
            filename = self.ui.lineEdit_FileName.text().strip()
            sheetname = self.ui.lineEdit_SheetName.text().strip()
            isAdd = self.ui.checkBox_IsAdded.isChecked()
            firstClient = self.ui.checkBox_FirstClient.isChecked()
            clientRow = int(self.ui.lineEdit_ClientRow.text())
            codeCol = int(self.ui.lineEdit_CodeCol.text())
            codeBits = R.toInt(self.ui.lineEdit_CodeBits.text())
            codeBits = None if (codeBits == -1 or codeBits == 0) else codeBits
            startRow = int(self.ui.lineEdit_StartRow.text())
            endRow = int(self.ui.lineEdit_EndRow.text())
            startCol = int(self.ui.lineEdit_StartColumn.text())
            endCol = int(self.ui.lineEdit_EndColumn.text())
            a = ExcelAtuoInput(
                filename=filename,  # 录入文件路径
                sheetname=sheetname,  # 表名
                client_row=clientRow,  # 客户编码行
                code_col=codeCol,  # 征订代码列
                start_row=startRow,  # 起始行
                end_row=endRow,  # 结束行
                start_col=startCol,  # 起始列
                end_col=endCol,  # 结束列
                code_number=codeBits,  # 教辅：None， 教材：6
                decrease=not isAdd    # isback = True: 调减需关闭输入法， False: 添加录入`
            )  

            self.hide()  # 不可省

            if firstClient:
                a.InputCols()
            else:
                a.InputRows()

        except Exception as e:
            QMessageBox.about(self, R.AppName, f"请填充正确的数字！Errors: {e}")

    
    def Refund(self):
        '''
        销退录入
        '''
        try:
            filename = self.ui.lineEdit_FileName.text().strip()
            sheetname = self.ui.lineEdit_SheetName.text().strip()
            isAdd = self.ui.checkBox_IsAdded.isChecked()
            firstClient = self.ui.checkBox_FirstClient.isChecked()
            clientRow = int(self.ui.lineEdit_ClientRow.text())
            codeCol = int(self.ui.lineEdit_CodeCol.text())
            codeBits = R.toInt(self.ui.lineEdit_CodeBits.text())
            codeBits = None if (codeBits == -1 or codeBits == 0) else codeBits
            startRow = int(self.ui.lineEdit_StartRow.text())
            endRow = int(self.ui.lineEdit_EndRow.text())
            startCol = int(self.ui.lineEdit_StartColumn.text())
            endCol = int(self.ui.lineEdit_EndColumn.text())
            a = ExcelAtuoInput(
                filename=filename,  # 录入文件路径
                sheetname=sheetname,  # 表名
                client_row=clientRow,  # 客户编码行
                code_col=codeCol,  # 征订代码列
                start_row=startRow,  # 起始行
                end_row=endRow,  # 结束行
                start_col=startCol,  # 起始列
                end_col=endCol,  # 结束列
                code_number=codeBits,  # 教辅：None， 教材：6
                decrease=not isAdd    # isback = True: 调减需关闭输入法， False: 添加录入`
            )  

            self.hide()  # 不可省
            a.InputCols_Refund()

        except Exception as e:
            QMessageBox.about(self, R.AppName, f"请填充正确的数字！Errors: {e}")